package com.agu.mgt.fsmsmgt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.entity.fsmsmgt.FsmsTemplateCanvasEntity;
import com.agu.entity.fsmsmgt.FsmsTemplateEntity;
import com.agu.mgt.fsmsmgt.service.FsmsTemplateService;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.Query;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 模板表
 * 
 * @author 
 * @email 
 * @date 2019-01-18 13:37:03
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("模板表")
@RequestMapping("fsmstemplate")
public class FsmsTemplateController extends AbstractController{
	@Autowired
	private FsmsTemplateService fsmsTemplateService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("fsmstemplate:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<FsmsTemplateEntity> page = fsmsTemplateService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{templateId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("fsmstemplate:info")
	public Result info(@PathVariable("templateId") Integer templateId){
		FsmsTemplateEntity fsmsTemplate = fsmsTemplateService.queryObject(templateId);
		
		return ResultGenerator.successData(fsmsTemplate);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增模板表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("fsmstemplate:save")
	public Result save(@RequestBody FsmsTemplateEntity fsmsTemplate){
	
		fsmsTemplateService.save(fsmsTemplate);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("templateId","模板ID");
		     cmap.put("templateName","模板名称");
		     cmap.put("templateType","模板类别");
		     cmap.put("canvasNum","区块数");
		     cmap.put("apiFlag","API标识");
		     cmap.put("spId","spId");
		     cmap.put("spName","sp名称");
		     cmap.put("userId","创建用户");
		     cmap.put("approvalTime","审批时间");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsTemplate, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改模板表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("fsmstemplate:update")
	public Result update(@RequestBody FsmsTemplateEntity fsmsTemplate){
	
		FsmsTemplateEntity fsmsTemplate1 = fsmsTemplateService.queryObject(fsmsTemplate.getTemplateId());
	
		List<FsmsTemplateCanvasEntity> list = fsmsTemplate1.getCavnenList();
		
		Set<Integer> idList = new HashSet();
		
		for(FsmsTemplateCanvasEntity cavas: list)
		{
			idList.add(cavas.getCanvasId());
		}
		
		fsmsTemplateService.update(fsmsTemplate,idList);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("templateId","模板ID");
		     cmap.put("templateName","模板名称");
		     cmap.put("templateType","模板类别");
		     cmap.put("canvasNum","区块数");
		     cmap.put("apiFlag","API标识");
		     cmap.put("spId","spId");
		     cmap.put("spName","sp名称");
		     cmap.put("userId","创建用户");
		     cmap.put("approvalTime","审批时间");
		     cmap.put("createTime","创建时间");
	
		String remark = CompareUtils.getTwoObjectRemark(fsmsTemplate1, fsmsTemplate, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除模板表")
	@GetMapping("/delete/{templateId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmstemplate:delete")
	public Result delete(@PathVariable("templateId") Integer templateId){
	
	    	if (templateId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		FsmsTemplateEntity fsmsTemplate = fsmsTemplateService.queryObject(templateId);
	
		fsmsTemplateService.delete(templateId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("templateId","模板ID");
		     cmap.put("templateName","模板名称");
		     cmap.put("templateType","模板类别");
		     cmap.put("canvasNum","区块数");
		     cmap.put("apiFlag","API标识");
		     cmap.put("spId","spId");
		     cmap.put("spName","sp名称");
		     cmap.put("userId","创建用户");
		     cmap.put("approvalTime","审批时间");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsTemplate, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除模板表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmstemplate:delete")
	public Result deletes(@RequestBody Integer[] templateIds){
	
		fsmsTemplateService.deleteBatch(templateIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("fsmstemplate:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<FsmsTemplateEntity> list = fsmsTemplateService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> templateTypeMap = new HashMap<Integer, String>();

                templateTypeMap.put(1, "系统内置");
                templateTypeMap.put(2, "厂家定义");
            map.put("templateTypeMap", templateTypeMap);
               Map<Integer, String> apiFlagMap = new HashMap<Integer, String>();

                apiFlagMap.put(1, "支持API");
                apiFlagMap.put(2, "不支持API");
            map.put("apiFlagMap", apiFlagMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("模板ID", "templateId", 1);
			clist.add(cloumn);
            cloumn = new Column("模板名称", "templateName", 1);
			clist.add(cloumn);
            cloumn = new Column("模板类别", "templateType", 2);
			clist.add(cloumn);
            cloumn = new Column("区块数", "canvasNum", 1);
			clist.add(cloumn);
            cloumn = new Column("API标识", "apiFlag", 2);
			clist.add(cloumn);
            cloumn = new Column("spId", "spId", 1);
			clist.add(cloumn);
            cloumn = new Column("sp名称", "spName", 1);
			clist.add(cloumn);
            cloumn = new Column("创建用户", "userId", 1);
			clist.add(cloumn);
            cloumn = new Column("审批时间", "approvalTime", 1);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
